package com.etc.service.impl;

import com.etc.dao.CinemaDao;
import com.etc.dao.MovieDao;
import com.etc.entity.Cinema;
import com.etc.entity.Movie;
import com.etc.service.CinemaService;
import com.etc.service.MovieService;
import com.etc.util.PageData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

import java.text.DateFormat;
import java.util.Calendar;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * Created by Administrator on 2019/5/21.
 */
@Service
public class CinemaServiceImpl implements CinemaService {
    @Autowired
    CinemaDao cinemaDao;
    @Autowired
    MovieDao movieDao;


    @Override
    public Cinema addcinema(Cinema cinema) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(cinema.getMstartime());//设置calendar的初始时间为 mstartime
        //获取电影的releasedtime
        Movie movie = movieDao.findOne(cinema.getMid());
        if (movie.getMstatus()==2){//如果电影是还没上映的
            //提取上映时间的日期
            Pattern pattern01 = Pattern.compile("\\d{4}-\\d{2}-\\d{2}");
            Matcher matcher = pattern01.matcher(movie.getMreleasedtime());
            while (matcher.find()){
                DateFormat df = DateFormat.getDateInstance();

                try{
                    if (df.parse(matcher.group(0)).after(cinema.getMstartime())){//如果movie的上映时间在 排片的startime之后的话就错了
                        return null;
                    }
                }
                catch (Exception e){

                }
            }
        }

        //获取影片的时长
        Pattern pattern = Pattern.compile("\\d+");
        Matcher matcher = pattern.matcher(movie.getMplaytime());
        while (matcher.find()) {//字符串清洗，提取出片长的数字
            calendar.add(Calendar.MINUTE, Integer.parseInt(matcher.group(0)));
            cinema.setMendtime(calendar.getTime());
        }

        //设置 手动排片的 开始时间和结束时间
        Calendar addStart = Calendar.getInstance();
        addStart.setTime(cinema.getMendtime());
        Calendar addEnd = Calendar.getInstance();
        addEnd.setTime(cinema.getMendtime());
        //循环列出 这个cinema 的每部电影
        List<Cinema> list = cinemaDao.findByCname(cinema.getCname());
        for (Cinema c : list) {
            //判断 排片的 startime 和 endtime是否在  已排的片的 startime和endtime范围内
            Calendar start = Calendar.getInstance();
            if (c.getMstartime() == null && c.getMendtime() == null) {
                return cinemaDao.save(cinema);
            }
            start.setTime(c.getMstartime());
            start.add(Calendar.MINUTE, -10);

            Calendar end = Calendar.getInstance();
            end.setTime(c.getMendtime());
            end.add(Calendar.MINUTE, 10);
            if (addStart.after(start) && addStart.before(end)) {
                return null;
            }
            if (addEnd.after(start) && addEnd.before(end)) {
                return null;
            }
        }
        return cinemaDao.save(cinema);
    }

    @Override
    public boolean deletecinema(Integer cid) {
        cinemaDao.delete(cid);
        return false;
    }

    @Override
    public Cinema updatecinema(Cinema cinema) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(cinema.getMstartime());//设置calendar的初始时间为 mstartime
        //获取电影的releasedtime
        Movie movie = movieDao.findOne(cinema.getMid());
        if (movie.getMstatus()==2){
            //提取上映时间的日期
            Pattern pattern01 = Pattern.compile("\\d{4}-\\d{2}-\\d{2}");
            Matcher matcher = pattern01.matcher(movie.getMreleasedtime());
            while (matcher.find()){
                DateFormat df = DateFormat.getDateInstance();

                try{
                    if (df.parse(matcher.group(0)).after(cinema.getMstartime())) {
                        return null;
                    }
                }catch (Exception e){

                }
            }
        }
        //获取影片的时长
        Pattern pattern = Pattern.compile("\\d+");
        Matcher matcher = pattern.matcher(movie.getMplaytime());
        while (matcher.find()) {//字符串清洗，提取出片长的数字
            calendar.add(Calendar.MINUTE, Integer.parseInt(matcher.group(0)));
            cinema.setMendtime(calendar.getTime());
        }

        //设置 手动排片的 开始时间和结束时间
        Calendar addStart = Calendar.getInstance();
        addStart.setTime(cinema.getMendtime());
        Calendar addEnd = Calendar.getInstance();
        addEnd.setTime(cinema.getMendtime());
        //循环列出 这个cinema 的每部电影
        List<Cinema> list = cinemaDao.findByCname(cinema.getCname());
        cinema.getCid();
        for (Cinema c : list) {
            //判断 排片的 startime 和 endtime是否在  已排的片的 startime和endtime范围内
            if (c.getCid()!=cinema.getCid()){

                Calendar start = Calendar.getInstance();
                if (c.getMstartime() == null && c.getMendtime() == null) {
                    return cinemaDao.save(cinema);
                }
                start.setTime(c.getMstartime());
                start.add(Calendar.MINUTE, -10);

                Calendar end = Calendar.getInstance();
                end.setTime(c.getMendtime());
                end.add(Calendar.MINUTE, 10);
                if (addStart.after(start) && addStart.before(end)) {
                    return null;
                }
                if (addEnd.after(start) && addEnd.before(end)) {
                    return null;
                }
            }
        }
        return cinemaDao.save(cinema);
    }

    @Override
    public Cinema getcinema(Integer cid) {
        return cinemaDao.findOne(cid);
    }

    @Override
    public List<Cinema> getcinema(String cname) {
        return cinemaDao.findByCname(cname);
    }

    @Override
    public List<Cinema> findAllcname() {
        return cinemaDao.findAllcname();
    }

    @Override
    public PageData<Cinema> getCinemaByPage(int page, int pageSize, String content) {
        int pageIndex = 0;
        if (page >= 1) {
            pageIndex = page - 1;
        }
        Pageable pageable = new PageRequest(pageIndex, pageSize);
        Page page1 = cinemaDao.findByLike(content, pageable);
        PageData<Cinema> pd = new PageData<Cinema>(page1.getContent(), (int) page1.getTotalElements(), pageSize, page);

        return pd;
    }

    /**
     * @return 正在上映+即将上映的电影  用于排片
     */
    @Override
    public List<Movie> showAddList() {
        List<Movie> list01 = movieDao.listRecentMovie();
        for (Movie movie:list01){
            movie.setMname(movie.getMname()+"("+movie.getMreleasedtime()+")");
        }
        List<Movie> list02 = movieDao.findByMreleasedtimeContaining("选座购票");

        if (list02.addAll(list01)) {
            return list02;
        }
        return null;
    }
}
